package com.yiyahui.project.erp.controller.business;

import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.YiyaMarketReservation;
import com.yiyahui.project.erp.domain.requestVo.SearchRequest;
import com.yiyahui.project.erp.domain.responseVo.SearchResponse;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import com.yiyahui.project.erp.service.IYiyaMarketReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api("自定义")
@RestController
@RequestMapping("/erp/extend")
public class BusinessExtendController extends BaseController{

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;

    @Autowired
    private IYiyaMarketReservationService yiyaMarketReservationService;

    /**
     * 搜索市场建档和患者
     */
    @ApiOperation("搜索市场建档和患者")
    @PostMapping("/search")
    public SearchResponse searchPerson(@RequestBody SearchRequest searchRequest) {
        List<YiyaClinicPerson> yiyaClinicPersons = yiyaClinicPersonService.selectByNameOrPhone(searchRequest);
        List<String> caseNums = yiyaClinicPersons.stream().map(YiyaClinicPerson::getCaseNumber).collect(Collectors.toList());
        List<YiyaMarketReservation> yiyaMarketReservations = yiyaMarketReservationService.selectByNameOrPhone(searchRequest);
        List<YiyaMarketReservation> result = yiyaMarketReservations.stream().filter(r -> !caseNums.contains(r.getCaseNumber())).collect(Collectors.toList());
        SearchResponse searchResponse = new SearchResponse();
        searchResponse.setYiyaClinicPersons(yiyaClinicPersons);
        searchResponse.setYiyaMarketReservations(result);
        return searchResponse;
    }
}
